﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Auditing;
using Abp.Web.Models;
using FwProject.Com.Dto;
using FwProject.Controllers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Shop.Controllers
{
    /// <summary>
    /// 订单管理
    /// </summary>
    public class OrderController : FwProjectControllerBase
    {
        private readonly IShopOrderAppService _shopOrderAppService;

        private readonly IHostingEnvironment _env;

        public OrderController(IShopOrderAppService shopOrderAppService, IHostingEnvironment env)
        {
            _shopOrderAppService = shopOrderAppService;
            _env = env;
        }

        #region 订单列表
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index(string type = "")
        {
            ViewBag.Type = type;
            return View();
        }

        /// <summary>
        /// 代发货列表
        /// </summary>
        /// <returns></returns>
        public IActionResult ShipIndex()
        {
            return View();
        }

        public async Task<IActionResult> OrderDetail(Guid id)
        {
            var list = await _shopOrderAppService.GetOrderDetailsByOrderId(id);
            return View(list);
        }

        //public async Task<IActionResult> OrderHandle(Guid id)
        //{
        //    var entity = await _shopOrderAppService.GetShopOrderHandleDtoByOrderId(id);
        //    return View(entity);

        //}

        public async Task<IActionResult> OrderHandle(Guid id,string orderNumber)
        {
            var entity= new ShopOrderHandleDto();
            if (id!=Guid.Empty)
            {
                entity = await _shopOrderAppService.GetShopOrderHandleDtoByOrderId(id);
            }
            if (!string.IsNullOrEmpty(orderNumber))
            {
                entity = await _shopOrderAppService.GetShopOrderHandleDtoByOrderNumber(orderNumber);
            }
            return View(entity);

        }

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetOrderData(int page, int limit, ShopOrderFilter filter = null)
        {
            var data = await _shopOrderAppService.GetShopOrderLayerDataAsync(page, limit, filter);

            return Json(data);
        }
        #endregion

        #region 订单发货
        public IActionResult OrderShip(Guid id)
        {
            return View(id);
        }

        [HttpPost]
        public async Task<IActionResult> OrderShip(List<Guid> ids)
        {
            await _shopOrderAppService.OrderShipAsync(ids);
            return Json("0");
        }

        #endregion

        #region 订单收货
        public IActionResult OrderReceipt(Guid id)
        {
            return View(id);
        }

        [HttpPost]
        public async Task<IActionResult> OrderReceipt(List<Guid> ids)
        {
            await _shopOrderAppService.OrderReceiptAsync(ids);
            return Json("0");
        }
        #endregion

        #region 申请售后
        public IActionResult OrderAfterSale(Guid id)
        {
            return View(id);
        }

        public async Task<IActionResult> OrderAfterSale(List<Guid> ids)
        {
            await _shopOrderAppService.OrderAfterSaleAsync(ids);
            return Json("0");
        }

        #endregion

        #region 取消订单
        public IActionResult OrderCancel(Guid id)
        {
            //await _shopOrderAppService.OrderCancelAsync(id);
            return Json("0");
        }
        #endregion

        [HttpGet]
        [DisableAuditing]
        [WrapResult(false, false)]//关闭ABP的返回数据封装
        public async Task<IActionResult> GetOrderDetalisData(Guid id, ShopOrderStatus status)
        {
            var data = await _shopOrderAppService.GetOrderDetailsByOrderIdAsync(id, status);
            return Json(data);
        }


        public async Task<IActionResult> ExportOrderData(ShopOrderStatus? status, string keyword = "")
        {
            var data = await _shopOrderAppService.GetExportDataAsync(status, keyword);

            return DoExcel<ShopOrderExportDto>(data, _env);
        }
    }
}